package com.xiaoran.robot.controller;

import com.xiaoran.robot.entity.Msg;
import com.xiaoran.robot.service.impl.AuthServiceImpl;
import com.xiaoran.robot.utils.*;

import java.util.ArrayList;
import java.util.Arrays;

public class GroupMessageAdaptor {
    private final Msg msg;
    private final String activeChar = YamlReader.instance.getValueByKey("mirai.activeChar").toString();
    private final AuthServiceImpl authService = SpringUtil.getBean(AuthServiceImpl.class);

    //加载词库
    public GroupMessageAdaptor(Msg msg){
        this.msg = msg;
    }

    /**
     * 消息处理
     */
    public void handleMessage(){
        String qid = msg.getSenderInfo().getQid();
        String gid = msg.getSenderInfo().getGid();
        if (!authService.doAuth(gid, 0)){
            return;
        }
        String plainMsg = msg.getPlainMsg();
        boolean needCondition = msg.getNeedCondition();
        boolean activeDicts = msg.getActiveDicts();
        boolean isAt = msg.getIsAt();
        Integer loopNum = msg.getLoopNum();
        DictsParser dictsParser = null;

        if (activeDicts  && !needCondition) {
            // 无需触发条件的 key
            dictsParser = new DictsParser(1);
        } else if((isAt || needCondition) && activeDicts){
            dictsParser = new DictsParser(0);
        } else {
            // ai闲聊
                String[] keyToe = {"闲聊", "无聊", "好无聊", "我好无聊"};
                String[] keyEnd = {"gun", "滚", "sb", "闭嘴"};
                if(Arrays.asList(keyEnd).contains(plainMsg)){
                    if(Cache.get(gid) != null) {
                        new MessageSender(gid, "group")
                                .addContent(new RstMsgUtils().addText("我走了，这一走就是一辈子！别想我，想我我也不会回来的。").build()).sendMessage();
                        Cache.remove(gid);
                    }
                }
                if (Arrays.asList(keyToe).contains(plainMsg)) {
                    if(Cache.get(gid) == null) {
                        new MessageSender(gid, "group")
                                .addContent(new RstMsgUtils().addText("无聊了吗？小牛马替您解闷。").build()).sendMessage();
                    }
                    Cache.put(gid, 1, 5 * 60);
                } else {
                    if(Cache.get(gid) != null) {
                        String answer = StringUtils.getAnswer(plainMsg);
                        if ("未获取到相关信息".equals(answer)) {
                            return;
                        }
                        ArrayList<Object> rstMsg = new RstMsgUtils().atQQ(qid).addText("\n").addText(answer).build();
                        new MessageSender(gid, "group").addContent(rstMsg).sendMessage();
                        Cache.put(gid, 1, 5 * 60); // 刷新时间
                    }
                }
        }
        if (dictsParser!=null && dictsParser.containsKey(plainMsg)) {
            // 得到要发送的消息了，接下来就是发送的消息的组成。
            for (int i = 0; i < loopNum; i++) {
                // 这里是精确查找关键词，后期应该增加模糊查找 类似： 猜拳 石头 100
                ArrayList<Object> val = dictsParser.getVal(plainMsg, "group", qid);
                new MessageSender(gid, "group").addContent(val).sendMessage();
            }
        }
    }


}
